#include <force/floppy3.h>
#include <force/fat12.h>
#include <funcs.h>



typedef unsigned char	uint8_t;	/* or #include <stdint.h> */
typedef unsigned short	uint16_t;	/* Note: multi-byte values are little-endian */
typedef unsigned long	uint32_t;

                     /* 32 bytes total */

struct fat_bootsector			/* Warning: this struct must be packed */
{
	uint8_t		jump[3];               /* 16-bit JMP to boot code, or 8-bit JMP + NOP */
	uint8_t		oem_id[8];             /* e.g. 'MSWIN4.0' */
	uint16_t	bytes_per_sector;	/* usu. =512 */
	uint8_t		sectors_per_cluster;
	uint16_t	num_boot_sectors;	/* usu. =1 */
	uint8_t		num_fats;              /* usu. =2 */
	uint16_t	num_root_dir_ents;
	uint16_t	total_sectors;         /* 16-bit; 0 if num sectors > 65535 */
	uint8_t 	media_ID_byte;         /* usu. =0F0h */
	uint16_t	sectors_per_fat;
	uint16_t	sectors_per_track;
	uint16_t	heads;
	uint32_t	hidden_sectors;	/* =LBA partition start */
	uint32_t	total_sectors_large;   /* 32-bit; 0 if num sectors < 65536 */
	uint8_t		boot_code[474];
	uint8_t		magic[2];              /* 55h, 0AAh */
};                              /* 512 bytes total */


struct fat_dirent               	/* Warning: this struct must be packed */
{
	uint8_t  name[8];               /* ALL-CAPS, pad right with spaces */
	uint8_t  ext[3];                /* ALL-CAPS, pad right with spaces */
	uint8_t  attrib;                /* attribute byte */
	uint8_t  reserved;              /* =0 */
	uint8_t  ctime_ms;              /* file creation time, 10ms units */
	uint16_t ctime;              	/* file creation time, in DOS format */
	uint16_t cdate;              	/* file creation date, in DOS format */
	uint16_t adate;              	/* DOS date of last file access */
	uint16_t st_clust_msw;       	/* high 16 bits of starting cluster (FAT32) */
	uint16_t mtime;              	/* DOS time of last file modification */
	uint16_t mdate;              	/* DOS date of last file modification */
	uint16_t st_clust;           	/* starting cluster */
	uint32_t file_size;          	/* in bytes */
};
struct fat_direntry 
	{
   unsigned char name[11];
   unsigned char attr;
   char _reserved[10];
   unsigned short time;
   unsigned short date;
   unsigned short cluster;
   unsigned size;
    };
struct attrib                   /* Warning: this struct must be packed */
{
	int read_only : 1;      /* b0 */
	int hidden : 1;
	int system : 1;
	int volume_label : 1;
	int directory : 1;
	int archive : 1;
	int reserved : 2;       /* b6, b7 */
};                              /* 1 byte total */

struct dos_time                 /* Warning: this struct must be packed */
{
	unsigned two_secs : 5;  /* low 5 bits: 2-second increments */
	unsigned minutes : 6;   /* middle 6 bits: minutes */
	unsigned hours : 5;     /* high 5 bits: hours (0-23) */
};                              /* 2 bytes total */

struct dos_date                 /* Warning: this struct must be packed */
{
	unsigned date : 5;      /* low 5 bits: date (1-31) */
	unsigned month : 4;     /* middle 4 bits: month (1-12) */
	unsigned year : 7;      /* high 7 bits: year - 1980 */
};                 

struct floppyDisk
{
    short bytesPerSector;
    char sectorsPerCluster;
    short reservedSectors;
    char numOfFats;
    short maxRootEntries;
    short totalSectors;
    short sectorsPerFat;
    short sectorsPerTrack;
    short numberOfHeads;
    char bootSig;
    int volumeId;
    char volumeLabel[12];
};



struct floppyDisk floppy1;

void get_bootsector()
{


	
	char bootsector[512];
    floppy_read_sectors(bootsector, 0, 1);
	
	kprintf("Fat Driver v0.1 alfa [");
	
	int ii;
	for (ii=3;ii<11;ii++)
	kprintf("%c",bootsector[ii]);
	
	kprintf("]\n");
	
    floppy1.bytesPerSector = (short)bootsector[11];
    floppy1.bytesPerSector |= (short)bootsector[12] << 8;
    floppy1.sectorsPerCluster = bootsector[13];
    floppy1.reservedSectors = (short)bootsector[14];
    floppy1.reservedSectors |= (short)bootsector[15] << 8;
    floppy1.numOfFats = bootsector[16];
    floppy1.maxRootEntries = (short)bootsector[17];
    floppy1.maxRootEntries |= (short)bootsector[18] << 8;
    floppy1.totalSectors = (short)bootsector[19];
    floppy1.totalSectors |= (short)bootsector[20] << 8;
    floppy1.sectorsPerFat = (short)bootsector[22];
    floppy1.sectorsPerFat |= (short)bootsector[23] << 8;
    floppy1.sectorsPerTrack = (short)bootsector[24];
    floppy1.sectorsPerTrack |= (short)bootsector[25] << 8;
    floppy1.numberOfHeads = (short)bootsector[26];
    floppy1.numberOfHeads |= (short)bootsector[27] << 8;
    floppy1.bootSig = bootsector[38];
    floppy1.volumeId = (int)bootsector[39];
    floppy1.volumeId |= (int)bootsector[40] << 8;
    floppy1.volumeId |= (int)bootsector[41] << 16;
    floppy1.volumeId |= (int)bootsector[42] << 24;
    floppy1.volumeLabel[0] = bootsector[43];
    floppy1.volumeLabel[1] = bootsector[44];
    floppy1.volumeLabel[2] = bootsector[45];
    floppy1.volumeLabel[3] = bootsector[46];
    floppy1.volumeLabel[4] = bootsector[47];
    floppy1.volumeLabel[5] = bootsector[48];
    floppy1.volumeLabel[6] = bootsector[49];
    floppy1.volumeLabel[7] = bootsector[50];
    floppy1.volumeLabel[8] = bootsector[51];
    floppy1.volumeLabel[9] = bootsector[52];
    floppy1.volumeLabel[10] = bootsector[53];
    floppy1.volumeLabel[11] = '\0';
}


void readRoot()
{
	dir = malloc(0x1C00);
	floppy_read_sectors((unsigned char *)dir, 19, 14);
	kprintf("%c",(dir->name[0]));
}
